package cn.iocoder.yudao.module.alipay.controller.admin.cpssource;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.cpssource.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.cpssource.CpsSourceDO;
import cn.iocoder.yudao.module.alipay.convert.cpssource.CpsSourceConvert;
import cn.iocoder.yudao.module.alipay.service.cpssource.CpsSourceService;

@Tag(name = "管理后台 - 渠道标识")
@RestController
@RequestMapping("/alipay/cps-source")
@Validated
public class CpsSourceController {

    @Resource
    private CpsSourceService cpsSourceService;

    @PostMapping("/create")
    @Operation(summary = "创建渠道标识")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:create')")
    public CommonResult<Long> createCpsSource(@Valid @RequestBody CpsSourceCreateReqVO createReqVO) {
        return success(cpsSourceService.createCpsSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新渠道标识")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:update')")
    public CommonResult<Boolean> updateCpsSource(@Valid @RequestBody CpsSourceUpdateReqVO updateReqVO) {
        cpsSourceService.updateCpsSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除渠道标识")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:delete')")
    public CommonResult<Boolean> deleteCpsSource(@RequestParam("id") Long id) {
        cpsSourceService.deleteCpsSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得渠道标识")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:query')")
    public CommonResult<CpsSourceRespVO> getCpsSource(@RequestParam("id") Long id) {
        CpsSourceDO cpsSource = cpsSourceService.getCpsSource(id);
        return success(CpsSourceConvert.INSTANCE.convert(cpsSource));
    }

    @GetMapping("/list")
    @Operation(summary = "获得渠道标识列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:query')")
    public CommonResult<List<CpsSourceRespVO>> getCpsSourceList(@RequestParam("ids") Collection<Long> ids) {
        List<CpsSourceDO> list = cpsSourceService.getCpsSourceList(ids);
        return success(CpsSourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getListByUserId")
    @Operation(summary = "根据userid获得渠道标识列表")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:query')")
    public CommonResult<List<CpsSourceRespVO>> getListByUserId(@RequestParam("userId")String userId) {
        List<CpsSourceDO> list = cpsSourceService.getListByUserId(userId);
        return success(CpsSourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得渠道标识分页")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:query')")
    public CommonResult<PageResult<CpsSourceRespVO>> getCpsSourcePage(@Valid CpsSourcePageReqVO pageVO) {
        PageResult<CpsSourceDO> pageResult = cpsSourceService.getCpsSourcePage(pageVO);
        return success(CpsSourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出渠道标识 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:cps-source:export')")
    @OperateLog(type = EXPORT)
    public void exportCpsSourceExcel(@Valid CpsSourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CpsSourceDO> list = cpsSourceService.getCpsSourceList(exportReqVO);
        // 导出 Excel
        List<CpsSourceExcelVO> datas = CpsSourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "渠道标识.xls", "数据", CpsSourceExcelVO.class, datas);
    }

}
